
package com.tools.cleanmaster.functionpage;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;


public class ScanMemoryView extends FrameLayout {
    private MemoryProgressCircle demoMpc;
    private AnimMemoryTextView demoTv;
    private int progress;

    public ScanMemoryView(Context ctx) {
        super(ctx);
        init();
    }

    public ScanMemoryView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        init();
    }

    private void init() {
        inflate(getContext(), com.tools.cleanmaster.R.layout.scan_memory_view, this);
        demoMpc = findViewById(com.tools.cleanmaster.R.id.demo_mpc);
        demoTv = findViewById(com.tools.cleanmaster.R.id.demo_tv);
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public void startAccAnim(AnimatorListenerAdapter listenerAdapter) {
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(demoMpc, "percent", 0, progress / 100f),
                ObjectAnimator.ofInt(demoTv, "progress", 0, progress)
        );
        set.setDuration(1000);
        if (null != listenerAdapter) {
            set.addListener(listenerAdapter);
        }
        set.setInterpolator(new AccelerateInterpolator());
        set.start();
    }

    public void startDecAnim(AnimatorListenerAdapter listenerAdapter) {
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(demoMpc, "percent", progress / 100f, 0),
                ObjectAnimator.ofInt(demoTv, "progress", progress, 0)
        );
        set.setDuration(1500);
        if (null != listenerAdapter) {
            set.addListener(listenerAdapter);
        }
        set.setInterpolator(new DecelerateInterpolator());
        set.start();
    }
}
